package com.example.linktop.logindemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private EditText editusername;
    private EditText editPwd;
    private CheckBox cb_remb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        editusername = (EditText) findViewById(R.id.username); //用户名
        editPwd = (EditText) findViewById(R.id.pwd);//密码
        cb_remb = (CheckBox) findViewById(R.id.remberCheckBox);


        //读取
        Map<String, String> maps = UserInfoUtils.readInfo(MainActivity.this);
        if (maps != null) {
            editusername.setText(maps.get("name"));
            editPwd.setText(maps.get("pwd"));

        }

    }

    /*
    * 登录点击事件
    *
    */

    public void loginFun(View V) {

        String usrName = editusername.getText().toString().trim();
        String pwd = editPwd.getText().toString().trim();

        if (TextUtils.isEmpty(usrName) || TextUtils.isEmpty(pwd)) {

            Toast.makeText(MainActivity.this,"用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        System.out.println("登录逻辑");

        if (cb_remb.isChecked()) {
//            保存用户名密码 data 目录
            boolean result = UserInfoUtils.saveInfo(MainActivity.this,usrName,pwd);

//            快速保存


            if (result) {

                Toast.makeText(MainActivity.this,"保存成功", Toast.LENGTH_SHORT).show();

            } else  {


                Toast.makeText(MainActivity.this,"保存失败!", Toast.LENGTH_SHORT).show();
            }

        }



    }

}
